<template>
  <div v-if="routeName === 'Manage'" class="list-page">
    <div class="tool-btns">
      <el-button
        type="primary"
        size="small"
        @click="$router.push({ name: 'AddManage' })"
        >创建管理员</el-button
      >
    </div>
    <div class="block-wrap filter-form">
      <el-form :inline="true" :model="form" ref="ruleForm" size="small">
        <el-form-item label="全部角色">
          <el-select v-model="form.roleId">
            <el-option
              v-for="item in roleList"
              :key="item.roleId"
              :label="item.roleName"
              :value="item.roleId"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="管理员名称">
          <el-input v-model="form.username" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="姓名">
          <el-input v-model="form.realName" placeholder="请输入" />
        </el-form-item>

        <el-form-item class="filter-form-btns">
          <el-button @click="search" type="primary">查询</el-button>
          <el-button type="default" @click="resetForm('ruleForm')"
            >重置</el-button
          >
        </el-form-item>
      </el-form>
    </div>
    <div class="block-wrap">
      <el-table
        :data="dataList"
        style="width: 100%;"
        align="center"
        header-align="center"
        :header-cell-style="headerStyle"
        :cell-style="cellStyle"
        v-loading="loading"
      >
        <el-table-column
          type="index"
          label="序号"
          width="60"
          :index="getIndex"
        />
        <el-table-column prop="username" label="管理员账户" />
        <el-table-column prop="realName" label="姓名" />
        <el-table-column prop="phone" label="手机号" />
        <el-table-column prop="rolenames" label="关联角色"> </el-table-column>
        <el-table-column prop="status" label="账户状态">
          <template slot-scope="scope">
            {{ getStatus(scope.row.status) }}
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间">
          <template slot-scope="scope">
            {{ getCreateTime(scope.row.createTime) }}
          </template>
        </el-table-column>
        <el-table-column prop="options" label="操作">
          <template slot-scope="scope">
            <div class="operation">
              <el-button type="text" @click="handleEdit(scope.row)"
                >编辑</el-button
              >
              <!-- <el-button type="text" @click="handleDelete(scope.row)"
              >删除</el-button
            > -->
              <el-switch
                class="switch"
                v-model="scope.row.status"
                active-value="1"
                inactive-value="9"
                active-text="启用"
                inactive-text="禁用"
                active-color="#1890FF"
                inactive-color="#BFBFBF"
                @change="updateUserStatus(scope.row)"
              >
              </el-switch>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :page-size="form.limit"
        :page-sizes="[10, 20, 30, 40]"
        layout="total, prev, pager, next, sizes, jumper"
        :total="total"
      />
    </div>
    <div class="footer">
      Copyright © 2018-2020 湖南智慧政务区块链科技有限公司
    </div>
  </div>
  <router-view v-else :key="key" />
</template>
<script>
import {
  getUserList,
  getRoleList,
  deleteUser,
  updateUserStatus
} from "@/api/sys";
import { parseTime } from "@/utils/index";
export default {
  name: "Manage",
  data() {
    return {
      form: {
        roleId: "", //角色名称
        username: "", //账号
        realName: "", //姓名
        page: 1,
        limit: 10
      },
      dataList: [],
      total: 0,
      // 所有角色
      roleList: [],
      statusSwitch: "100",
      loading: false
    };
  },
  computed: {
    key() {
      return this.$route.path;
    },
    routeName() {
      return this.$route.name;
    }
  },
  created() {
    this.getUserList();
    this.getRoleList();
  },
  methods: {
    getUserList() {
      this.loading = true;
      getUserList(this.form)
        .then(res => {
          let { records, total } = res.data;
          this.dataList = records;
          this.total = total;
          this.loading = false;
        })
        .catch(() => {
          this.loading = false;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    getStatus(status) {
      status = status.toString();
      switch (status) {
        case "9":
          return "禁用";
        case "1":
          return "启用";
      }
    },
    getCreateTime(time) {
      let createTime = new Date(time).getTime();
      return parseTime(createTime);
    },
    getRoleList() {
      getRoleList().then(res => {
        this.roleList = res.data;
      });
    },
    search() {
      this.getUserList();
    },
    handleSizeChange(val) {
      this.form.limit = val;
      this.getUserList();
    },
    handleCurrentChange(val) {
      this.form.page = val;
      this.getUserList();
    },
    // 重置表单查询条件
    resetForm(formName) {
      this.form = {
        roleId: "", //角色名称
        username: "", //账号
        realName: "", //姓名
        page: 1,
        limit: 10
      };
      this.getUserList();
    },
    //改变用户状态
    updateUserStatus(row) {
      console.log(row.status);
      let operation = row.status === "1" ? "启用" : "禁用";
      console.log(operation);
      this.$confirm(`你确认要${operation}${row.username}吗, 是否继续`, "提示", {
        cancelButtonText: "取消",
        confirmButtonText: "确定",
        type: "warning"
      })
        .then(() => {
          updateUserStatus(row.userId, row.status).then(res => {
            if (res.code === 200) {
              this.$message({
                type: "success",
                message: `成功${operation}!`
              });
              this.getUserList();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消"
          });
          this.getUserList();
        });
    },
    // 删除
    handleDelete(row) {
      this.$confirm("此操作将删除该用户, 是否继续", "提示", {
        cancelButtonText: "取消",
        confirmButtonText: "确定",
        type: "warning"
      })
        .then(() => {
          deleteUser(row.userId).then(res => {
            if (res.code === 200) {
              this.$message({
                type: "success",
                message: "删除成功!"
              });
              this.getUserList();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    // 编辑
    handleEdit(row) {
      this.$router.push({
        name: "AddManage",
        query: { userId: row.userId }
      });
    },
    headerStyle() {
      return {
        background: "#F5F9FF",
        color: "#2E2E30",
        textAlign: "center"
      };
    },
    cellStyle() {
      return {
        height: "48px",
        padding: "0",
        textAlign: "center"
      };
    },
    getIndex($index) {
      return $index + (this.form.page - 1) * this.form.limit + 1;
    }
  }
};
</script>

<style lang="scss" scoped>
@import "~@/styles/page/list-page.scss";
.list-page {
  .block-wrap {
    .el-pagination {
      ::v-deep.el-pagination__total {
        float: left;
      }
    }
  }
}
.block-wrap {
  padding: 25px;
  background: #fff;
  border-radius: 2px;
  margin-bottom: 16px;
}
.el-form--inline .el-form-item {
  margin-right: 60px;
}
::v-deep.list-page .filter-form .filter-form-btns {
  margin-right: 10px;
}
.el-table .el-table__cell {
  text-align: center;
}
.el-button--text {
  color: #5892ff;
}
::v-deep.el-pagination .btn-prev,
::v-deep.el-pagination .btn-next,
::v-deep.el-pagination .el-pager li {
  border: 1px solid #eee;
  margin-right: 8px;
}
::v-deep.el-pagination .el-pager {
  li.active + li {
    border-left: 1px solid #eee !important;
  }
}
::v-deep.el-pagination .el-pager {
  li.active {
    border-color: #295ec8 !important;
  }
}
.footer {
  text-align: center;
  margin-top: 50px;
  font-size: 12px;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  color: rgba(0, 0, 0, 0.45);
  line-height: 20px;
}
::v-deep.switch {
  margin-left: 24px;
  height: 40px;
  .el-switch__label {
    position: absolute;
    display: none;
    color: #fff !important;
  }
  /*打开时文字位置设置*/
  .el-switch__label--right {
    z-index: 9;
    span {
      margin-right: 9px;
      height: 20px;
      font-size: 12px;
      font-family: PingFangSC-Regular, PingFang SC;
      font-weight: 400;
      color: #ffffff;
      line-height: 20px;
    }
  }
  .el-switch__label--left {
    z-index: 9;
    span {
      margin-left: 9px;
      height: 20px;
      font-size: 12px;
      font-family: PingFangSC-Regular, PingFang SC;
      font-weight: 400;
      color: #ffffff;
      line-height: 20px;
    }
  }
  .el-switch__label.is-active {
    display: block;
  }
}
// /* 调整按钮的宽度 */
::v-deep.switch.el-switch .el-switch__core,
::v-deep.el-switch .el-switch__label {
  width: 58px !important;
  height: 21px;
  margin: 0;
}
.operation {
  display: flex;
  justify-content: center;
}
</style>
